package top.korori.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.korori.common.enums.IsDel;
import top.korori.common.exception.BizException;
import top.korori.common.model.PageData;
import top.korori.common.utils.EnumUtils;
import top.korori.entity.SvWork;
import top.korori.entity.req.SvHomeWorkGetReq;
import top.korori.entity.req.SvMyWorkGetReq;
import top.korori.entity.req.SvWorkAddReq;
import top.korori.entity.req.SvWorkGetReq;
import top.korori.entity.res.SvUserWorkRes;
import top.korori.entity.res.SvWorkRes;
import top.korori.enums.SvWorkPerEnum;
import top.korori.enums.SvWorkStatusEnum;
import top.korori.enums.WorkGetCategoryEnum;
import top.korori.enums.WorkGetSortEnum;
import top.korori.mapper.SvWorkMapper;
import top.korori.service.ISvWorkService;
import top.korori.uitls.SvSecurityUtil;


/**
 *
 * 作品 服务实现类
 * 
 *
 * @author korori
 */
@Service
public class SvWorkServiceImpl extends ServiceImpl<SvWorkMapper, SvWork> implements ISvWorkService {


    @Override
    public SvUserWorkRes push(SvWorkAddReq svWorkAddReq) {
        SvWork svWork = BeanUtil.copyProperties(svWorkAddReq, SvWork.class);
        svWork.setUserId(SvSecurityUtil.getUserId());
        svWork.setStatus(SvWorkStatusEnum.NORMAL.getCode());
        this.save(svWork);
        return BeanUtil.copyProperties(svWork, SvUserWorkRes.class);
    }


    @Override
    public PageData<SvUserWorkRes> getHoneWorks(SvHomeWorkGetReq svHomeWorkGetReq) {

        Page<SvUserWorkRes> svWorkVoPage = new Page<>(svHomeWorkGetReq.getPageNum(), svHomeWorkGetReq.getPageSize());

        Page<SvUserWorkRes> page = baseMapper.getHomeWorkExtUserList(svWorkVoPage);

        PageData<SvUserWorkRes> svWorkVoPageData = new PageData<>();
        svWorkVoPageData.setTotal(page.getTotal());
        svWorkVoPageData.setList(page.getRecords());
        return svWorkVoPageData;
    }

    @Override
    public void delMyWork(Long workId) {
        Long userId = SvSecurityUtil.getUserId();
        boolean flag = this.lambdaUpdate()
                .eq(SvWork::getWorkId, workId)
                .eq(SvWork::getUserId, userId)
                .set(SvWork::getIsDel, IsDel.DELETED.getCode()).update();
        if (!flag) BizException.cast("要删除的作品不存在");
    }

    @Override
    public PageData<SvWorkRes> getUserHomeList(SvWorkGetReq svWorkGetReq) {

        //校验参数
        if (!EnumUtils.existByValue(WorkGetCategoryEnum::getCode, svWorkGetReq.getCategory())){
            return new PageData<>();
        }

        if (ObjUtil.isNull(svWorkGetReq.getSort())){
            svWorkGetReq.setSort(WorkGetSortEnum.TIME.getCode());
        }

        //数据查询
        Page<SvWork> svMyWorkResPage = new Page<>(svWorkGetReq.getPageNum(),svWorkGetReq.getPageSize());
        LambdaQueryWrapper<SvWork> workQueryWrapper = new LambdaQueryWrapper<>();


        if (ObjUtil.equal(svWorkGetReq.getCategory(), WorkGetCategoryEnum.WORK.getCode())){

            if (ObjUtil.equal(SvSecurityUtil.getUserId(),svWorkGetReq.getUserId())){
                workQueryWrapper.eq(SvWork::getPer, SvWorkPerEnum.ALL.getCode());
            }else {
                workQueryWrapper.eq(SvWork::getPer, SvWorkPerEnum.ALL.getCode());
                workQueryWrapper.eq(SvWork::getStatus,SvWorkStatusEnum.NORMAL.getCode());
            }
        }

        if (ObjUtil.equal(svWorkGetReq.getCategory(), WorkGetCategoryEnum.PRIVATE.getCode())){
            workQueryWrapper.eq(SvWork::getPer, SvWorkPerEnum.PRIVACY.getCode());
        }

        if (ObjUtil.equal(svWorkGetReq.getSort(), WorkGetSortEnum.LIKE.getCode())){
            workQueryWrapper.orderByDesc(SvWork::getLike);
        }


        workQueryWrapper.orderByDesc(SvWork::getCreateTime);
        workQueryWrapper.eq(SvWork::getIsDel,IsDel.NORMAL.getCode());
        workQueryWrapper.eq(SvWork::getUserId,svWorkGetReq.getUserId());
        Page<SvWork> svWorkPage = baseMapper.selectPage(svMyWorkResPage, workQueryWrapper);

        return PageData.setDataCopyRes(svWorkPage,SvWorkRes.class);
    }

    @Override
    public PageData<SvWorkRes> getUserHomeList(SvMyWorkGetReq svMyWorkGetReq) {
        SvWorkGetReq svWorkGetReq = BeanUtil.copyProperties(svMyWorkGetReq, SvWorkGetReq.class);
        svWorkGetReq.setUserId(SvSecurityUtil.getUserId());
        return this.getUserHomeList(svWorkGetReq);
    }


}
















